﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using HandyControl.Controls;
using clf.weighing.entity;

namespace weighing
{
    /// <summary>
    /// PasswordWindow.xaml 的交互逻辑
    /// </summary>
    public partial class PasswordWindow : HandyControl.Controls.Window
    {
        public PasswordWindow()
        {
            InitializeComponent();

            CommonFunction.ShowCustomIcon(this);
        }

        private void SaveBtn_Click(object sender, RoutedEventArgs e)
        {
            String old = this.oldPwd_tb.Password.Trim();
            
            if (old.IsNullOrEmpty() || old.Length < 6)
            {
                Growl.Error("旧密码输入不正确，至少6 位");
                this.saveBtn.IsChecked = false;
                return;
            }


            String newPwd = this.newPwd_tb.Password.Trim();
            if (newPwd.IsNullOrEmpty() || newPwd.Length < 6)
            {
                Growl.Error("新密码输入不正确，至少6 位");
                this.saveBtn.IsChecked = false;
                return;
            }

            String newPwd2 = this.newPwd_tb2.Password.Trim();
            if (newPwd != newPwd2)
            {
                Growl.Error("两次新密码输入不正一至");
                this.saveBtn.IsChecked = false;
                return;
            }

            if (App.CurrentUser.password != MyHelper.EncryptHelper.MD5Encrypt(old))
            {
                Growl.Error("旧密码不正确！");
                this.saveBtn.IsChecked = false;
                return;
            }

            string newPassword = MyHelper.EncryptHelper.MD5Encrypt(newPwd);

            if (App.CurrentUser.password.Equals(newPassword))
            {
                Growl.Error("新密码输入不正确！");
                this.saveBtn.IsChecked = false;
                return;
            }

            var user = new User()
            {
                id = App.CurrentUser.id,
                password = newPassword,
                phone = App.CurrentUser.phone
            };
            int res = UserDao.Instance.UpdatePassword(user);
            if (res > 0)
            {
                try
                {
                    HistoryUserDao.Instance.UpdateUserPwd(user);
                }
                catch (Exception ex)
                {

                }              
                string msg = "密码修改成功！下次登陆有效";
                Growl.Success(msg);
                this.newPwd_tb.Clear();
                this.oldPwd_tb.Clear();
                this.newPwd_tb2.Clear();
                CommonFunction.AlertInfo(msg,this);
                this.Close();
            }
            else {
                Growl.Warning("密码修改失败！");
            }
            this.saveBtn.IsChecked = false;
        }

        private void cancelBtn_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
